
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

//首页路由模块
var indexRouter = require('./routes/index');
//列表路由模块
var listRouter = require('./routes/list');
//登录注册路由模块
var loginRouter = require('./routes/login');
//课程详情路由模块
var courseRouter = require('./routes/course');
//个人中心相关路由模块
var personRouter = require('./routes/person');

var cors=require('cors')

const { verify } = require('crypto');
const { token } = require('morgan');

const{}=require('./utils/config')

var app = express();

app.use(cors())

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/uploads' ,express.static(path.join(__dirname, 'uploads')));

app.use('/api', indexRouter);
app.use('/api', listRouter);
app.use('/api', loginRouter);
app.use('/api', courseRouter);
// app.use(function (req, res, next) {
//   // let token = req.headers.authorization
//   if (!verifyToken(token)) {
//     next({
//       msg: '请传递token',
//       status: 500
//     })
//     return
//   }
//   next()
// })

app.use('/api', personRouter);

app.use(function(req,res,next){
  let token =req.headers.authorization
  if(!verifyToken(token)){
    next({
      msg:'请传递token',
      status:500
    })
    return
  }
})




// catch 404 and forward to error handler
app.use(function(req, res, next) {
  // next(createError(404));
  res.send('404错误')
});

// error handler
app.use(function(err, req, res, next) {
  res.send(err)
});

module.exports = app;
